<?php

namespace app\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ArcTrain;

/**
 * ArcTrainSearchModel represents the model behind the search form of `app\models\ArcTrain`.
 */
class ArcTrainSearchModel extends ArcTrain
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id','progress'], 'integer'],
            [['arc_classify','arc_android'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ArcTrain::find();
        //联合查询
        $with = 'arcClassify';
        $with_android = 'arcAndroid';
        $query = $query->select('arc_train.*,arc_classify.name,arc_android.name')->joinWith($with, $eagerLoading = true ,$joinType = 'LEFT JOIN')->joinWith($with_android, $eagerLoading = true ,$joinType = 'LEFT JOIN');
        // $commandQuery = clone $query;
        // echo $commandQuery->createCommand()->getRawSql();
        // exit;
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            // 'id' => $this->id,
             'arc_classify' => $this->arc_classify,
             'arc_android' => $this->arc_android,
            'progress' => $this->progress,
        ]);


        /**
         * 实现模糊查询
         */
        $query->andFilterWhere(['like', 'arc_train.id', $this->id]);
        return $dataProvider;
    }
}
